package com.mysoft.gfyx.clue.controller;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.*;
import com.mysoft.gfyx.clue.mip.BigDataDeveloperService;
import com.mysoft.gfyx.clue.mip.SupplierDataService;
import com.mysoft.gfyx.clue.mip.dto.ImportDeveloperRequestDto;
import com.mysoft.gfyx.clue.mip.dto.ImportDeveloperResponseDto;
import com.mysoft.gfyx.clue.mip.dto.SupplierDataOneSupplierReqDto;
import com.mysoft.gfyx.clue.service.CustomerService;
import com.mysoft.gfyx.clue.service.dto.BusinessInfoDto;
import com.mysoft.gfyx.clue.service.dto.CustomerDto;
import com.mysoft.gfyx.clue.service.dto.CustomerMainDataDto;
import com.mysoft.gfyx.common.util.PermUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@PubService(value = "/customerDataApi", prefix = RequestPrefix.API)
@Tag(name = "客户管理-API")
public class CustomerDataApiController extends Controller implements DataApi {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private SupplierDataService supplierDataService;

    @Autowired
    private BigDataDeveloperService bigDataDeveloperService;

    @Autowired
    private PermUtil permUtil;

    @PubAction(value = "/getSupplierInfoByCode", method = RequestMethod.POST)
    public Object getSupplierInfoByCode(@RequestParam String code) {
        SupplierDataOneSupplierReqDto dto = new SupplierDataOneSupplierReqDto();
        dto.setProject_code("default_bigdata");
        dto.setIdentity_value(code);
        dto.setEntity_id("48b87112-fe95-11ec-afb4-ce3cc3e5a7f2");
        //dto.setTable_name("dim_ycg_common_developer");
        return supplierDataService.oneSupplier(dto);
    }

    /**
     * 查询工商列表 没用
     */
    @PubAction(value = "/flushContacterCount", method = RequestMethod.POST)
    public boolean flushContacterCount(String keyword) {
        return customerService.flushContacterCount();
    }

    /**
     *
     * 查询工商列表 没用
     */
//    @PubAction(value = "/searchBusinessInfo", method = RequestMethod.POST)
//    public List<BusinessInfoDto> searchBusinessInfo(String keyword) {
//        //调用工商接口。。。 todo
//        List<BusinessInfoDto> businessInfoDtos = new ArrayList<>();
//        for (int i = 0 ; i <= 10; i++) {
//            BusinessInfoDto businessInfoDto = new BusinessInfoDto();
//            businessInfoDto.setBusinessName("工商信息" + i);
//            businessInfoDtos.add(businessInfoDto);
//        }
//        return businessInfoDtos;
//    }

    /**
     * 获取客户主数据和反哺
     */
    @PubAction(value = "/getMainDataAndFeeding", method = RequestMethod.POST)
    public CustomerMainDataDto getMainDataAndFeeding(String company_name) {
        ImportDeveloperRequestDto dto = new ImportDeveloperRequestDto();
        CustomerMainDataDto customerMainDataDto = new CustomerMainDataDto();
        ImportDeveloperResponseDto importDeveloperResponseDto = bigDataDeveloperService.importDeveloper(company_name);
        if (importDeveloperResponseDto != null && importDeveloperResponseDto.getData() != null) {
            customerMainDataDto.setProvinceCode(importDeveloperResponseDto.getData().getProvince_code());
            customerMainDataDto.setProvinceName(importDeveloperResponseDto.getData().getProvince_name());//todo
            customerMainDataDto.setAreaCategoryCode(importDeveloperResponseDto.getData().getCity_code());
            customerMainDataDto.setAreaCategoryName(importDeveloperResponseDto.getData().getCity_name());
            customerMainDataDto.setCreditCode(importDeveloperResponseDto.getData().getCredit_code());
        }
        return customerMainDataDto;
    }

    /**
     * 查询工商列表 废弃
     */
    @PubAction(value = "/searchBusinessInfo", method = RequestMethod.POST)
    public List<BusinessInfoDto> searchBusinessInfo(String keyword) {
        //调用工商接口。。。 todo
        List<BusinessInfoDto> businessInfoDtos = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            BusinessInfoDto businessInfoDto = new BusinessInfoDto();
            businessInfoDto.setBusinessName("工商信息" + i);
            businessInfoDtos.add(businessInfoDto);
        }
        return businessInfoDtos;
    }

    /**
     * 批量添加到客户
     */
    @PubAction(value = "/batchAddCustomer", method = RequestMethod.POST)
    public boolean batchAddCustomer(@RequestBody List<BatchAddCustomerRequestDto> batchAddCustomerRequestDto) {
        for (int i = 0; i < batchAddCustomerRequestDto.size(); i++) {
            CustomerDto storeCustomerDto = MappingUtil.getSimpleMapperFacade().map(batchAddCustomerRequestDto.get(i), CustomerDto.class);
            CustomerDto customerDto = customerService.getCustomerWithUnique(storeCustomerDto.getCustomerResponsiblePersonGUID(), storeCustomerDto.getCreditCode());
            if (customerDto != null) {
                continue;
            }
            customerService.addCustomer(storeCustomerDto);
        }
        return true;
    }

    /**
     * 批量添加
     */
    @PubAction(value = "/addCustomers", method = RequestMethod.POST)
    public boolean addCustomer(@RequestBody PubAddCustomersRequestDto req) {
        boolean enableMarketingPerm = permUtil.checkBusinessUnit(permUtil.marketActionCode);
        if (!enableMarketingPerm) {
            throw new BusinessLogicException("当前操作没有权限，需要操作请购买【市场洞察】模块");
        }

        return customerService.addCustomerAndPool(req);
    }

    /**
     * 修改客户
     */
    @PubAction(value = "/editCustomer", method = RequestMethod.POST)
    public boolean editCustomer(@RequestBody EditCustomerRequestDto req) {
        return customerService.editCustomerAndPool(req);
    }

    /**
     * 删除客户
     */
    @PubAction(value = "/deleteCustomer", method = RequestMethod.POST)
    public boolean deleteCustomer(@RequestBody DeleteCustomerRequestDto req) {
        customerService.deleteCustomer(req);
        return true;
    }


}
